/*
 * Copyright 1999-2011 Alibaba Group.
 *  
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.alibaba.dubbo.common.io;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;

/**
 * UnsafeByteArrayOutputStream.
 * 
 * @author qian.lei
 */

public class UnsafeByteArrayOutputStream extends ByteArrayOutputStream{
	static public final int MIN_BUFFER_SIZE = 4096;
	
	protected byte mBuffer[];

	protected int mCount;
	
	protected int mMargin;
	
	public UnsafeByteArrayOutputStream()
	{
		this(MIN_BUFFER_SIZE);
    }
	

	public UnsafeByteArrayOutputStream(int size)
    {
		this(size, 0);
    }
	
	public UnsafeByteArrayOutputStream(int size, int margin)
    {
		if( size <= 0 )
			size = MIN_BUFFER_SIZE;
		if(margin < 0) margin = 0;
		this.mMargin = margin;
		mBuffer = new byte[size];
	}

	public void ensureIncreaseCapacity(int len){
		int newcount = mCount + len;
		if(this.mBuffer == null) this.mBuffer = ZERO;
		if(newcount > mBuffer.length){
			mBuffer = Bytes.copyOf(mBuffer, Math.max(mBuffer.length << 1,  newcount + this.mMargin));
		}
	}
	
	public void ensureCapacity(int len){
		int newcount = len;
		if(this.mBuffer == null) this.mBuffer = ZERO;
		if(newcount > mBuffer.length){
			mBuffer = Bytes.copyOf(mBuffer, Math.max(mBuffer.length << 1, newcount + this.mCount));
		}
	}
	
	public void write(int b)
	{
		ensureIncreaseCapacity(1);
		mBuffer[mCount] = (byte)b;
		mCount ++;
	}

	public void write(byte b[], int off, int len)
	{
		if( ( off < 0 ) || ( off > b.length ) || ( len < 0 ) || ( ( off + len ) > b.length ) || ( ( off + len ) < 0 ) )
			throw new IndexOutOfBoundsException();
		if( len == 0 )
			return;
		ensureIncreaseCapacity(len);
		System.arraycopy(b, off, mBuffer, mCount, len);
		mCount += len;
	}

	public int size()
	{
		return mCount;
	}

	public void reset()
	{
		mCount = 0;
	}

	public byte[] toByteArray()
	{
		if(this.mBuffer == null) this.mBuffer = ZERO;
		return Bytes.copyOf(mBuffer, mCount);
	}

	public ByteBuffer toByteBuffer()
	{
		if(mBuffer == null) return ByteBuffer.wrap(ZERO, 0, 0);
		return ByteBuffer.wrap(mBuffer, 0, mCount);
	}

	public void writeTo(OutputStream out) throws IOException
	{
		if(mBuffer != null){
			out.write(mBuffer, 0, mCount);
		}
	}

	public String toString()
	{
		if(this.mBuffer == null) this.mBuffer = ZERO;
		return new String(mBuffer, 0, mCount);
	}
	
	public ByteArray detach(){
		if(this.mBuffer == null) this.mBuffer = ZERO;
		ByteArray result = new ByteArray(this.mBuffer, 0, mCount);
		this.mBuffer = ZERO;
		mCount = 0;
		return result;
	}

	public String toString(String charset) throws UnsupportedEncodingException
	{
		if(this.mBuffer == null) this.mBuffer = ZERO;
		return new String(mBuffer, 0, mCount, charset);
	}
	
	
	public void writeInt8(int index, byte b){
		if(index >= mCount) throw new IndexOutOfBoundsException();
		mBuffer[index] = b;
	}
	
	public void writeInt16b(short b){
		this.ensureIncreaseCapacity(2);
		int index = mCount;
		Bytes.short2bytes(b, mBuffer, index);
		mCount += 2;
	}
	
	public void writeInt32b(int b){
		this.ensureIncreaseCapacity(4);
		int index = mCount;
		Bytes.int2bytes(b, mBuffer, index);
		mCount += 4;
	}
	
	
	 
	
	public void writeInt16b(int index, short b){
		if(index < 0 || (index + 2) > mCount) throw new IndexOutOfBoundsException();
		Bytes.short2bytes(b, mBuffer, index);
	}
	
	 
	
	public void writeInt32b(int index, int b){
		if(index < 0 || (index + 4) > mCount) throw new IndexOutOfBoundsException();
		Bytes.int2bytes(b, mBuffer, index);
	}
	
	 
	
	 
	public void writeInt64b(int index, long b){
		if((index + 8) > mCount) throw new IndexOutOfBoundsException();
		Bytes.long2bytes(b, mBuffer, index);
	}
	
	 
	
	 
	
	public void writeInt8(byte b){
		write(b);
	}
	
	public void writeInt8b(byte b){
		write(b);
	}
	
	public void writeInt8(int b){
		write(b);
	}
	
	public void writeInt8b(int b){
		write(b);
	}
	
	 
	 
	
	public void writeInt64b( long b){
		this.ensureIncreaseCapacity(8);
		Bytes.long2bytes(b,  mBuffer, mCount);
		mCount += 8;
	}

	public void close() throws IOException
	{
		mBuffer = ZERO;
		mCount = 0;
	}
	
	
}